package com.westar.base.businessUtil;

import com.alibaba.fastjson.JSONObject;
import com.westar.core.conf.SysConst;
import com.westar.base.pojo.WestarResult;
import com.westar.base.util.WestarHttpUtil;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;

import static com.westar.base.enums.HTTPResultEnum.CODE;
import static com.westar.base.enums.HTTPResultEnum.RESPONEBODY;

/**
 * 修改密码的工具类
 */
public class ChangePwdUtil {
    /**
     * 修改密码的工具
     *
     * @param actCookie 登录认证
     * @param passwd    密码
     */
    public static WestarResult autoChangePwd(String actCookie, String passwd) throws IOException {
        if(passwd.equals("111111")){
            return WestarResult.build(-1,"初始密码不能自动变更密码！");
        }
        Map<String, String> pageParam = ChangePwdUtil.getPageParam(actCookie);
        if (null != pageParam) {
            //是否包含有数据
            boolean containKey = pageParam.containsKey("oldPwd") && pageParam.containsKey("newPwd") && pageParam.containsKey("confirmPwd");
            if (!containKey) {
                return WestarResult.build(-1,"获取密码更新界面，页面有变动，不能获取到参数！");
            }
            pageParam.put("oldPwd", passwd);
            pageParam.put("newPwd", passwd);
            pageParam.put("confirmPwd", passwd);

            return ChangePwdUtil.doChangePwd(actCookie, pageParam);
        }else{
            return WestarResult.build(-1,"获取密码更新界面，请求错误！");
        }
    }

    /**
     * 开始修改密码
     *
     * @param actCookie 登录认证cookie
     * @param pageParam 参数信息
     */
    private static WestarResult doChangePwd(String actCookie, Map<String, String> pageParam) throws IOException {
        String url = "http://" + SysConst.DOMAIN + "/dataui/user/3.0/ChangePwd";
        Map<String, String> responeInfoMap = WestarHttpUtil.postMethodForAll(url, actCookie, pageParam);
        if (responeInfoMap.get(CODE.toString()).equals("200")) {
            String respnoeBody = responeInfoMap.get(RESPONEBODY.toString());
            JSONObject jsonObject = JSONObject.parseObject(respnoeBody);
            boolean success = jsonObject.getBoolean("success");
            String message = jsonObject.getString("message");
            if (!success) {
                return WestarResult.build(-1,message);
            }
            String isFirstLogin = jsonObject.getString("isFirstLogin");
            if (StringUtils.isNotEmpty(isFirstLogin) && isFirstLogin.equals("1")) {
                url = "http://" + SysConst.DOMAIN + "/oauth2/logout";
                WestarHttpUtil.getForAllMethod(url, actCookie, null);
            }
            return WestarResult.ok("更新密码操作成功！");
        } else {
            return WestarResult.build(-1,"更新密码，请求错误！");
        }
    }

    /**
     * 获取页面参数
     *
     * @param actCookie 登录认证cookie
     * @return
     */
    private static Map<String, String> getPageParam(String actCookie) throws IOException {
        //页面的已有字段
        Map<String, String> pageParam = new Hashtable<>();

        String url = "http://" + SysConst.DOMAIN + "/dataui/user/3.0/toChangePwd";
        Map<String, String> responeInfoMap = WestarHttpUtil.getForAllMethod(url, actCookie, null);
        if (responeInfoMap.get(CODE.toString()).equals("200")) {
            String respnoeBody = responeInfoMap.get(RESPONEBODY.toString());
            Document doc = Jsoup.parse(respnoeBody);
            Elements forms = doc.select("#userForm");
            if (null == forms || forms.isEmpty()) {
                return pageParam;
            }
            Elements inputs = forms.select("input");
            for (Element input : inputs) {
                String key = input.attr("name");
                String value = input.attr("value");
                pageParam.put(key, value);
            }
            return pageParam;
        } else {
            return null;
        }
    }
}
